<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>学生-录入实习信息</title>
    <link rel="stylesheet" href="${ctx}/static/layui/css/layui.css">
    <link rel="stylesheet" href="${ctx}/static/css/index.css">
    <style>
        .layui-form-item {
            margin: 8px 0px 8px 40px;
        }

        .layui-form-select dl {
            max-height: 260px;
        }
    </style>
</head>
<body>

<!-- 主体 -->
<div class="body-head"><span>实习日志管理&nbsp;>&nbsp;编写实习日志</span></div>
<form id="edit_form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block" style="width: 970px">
                <input type="text" name="name" id="name" autocomplete="off" class="layui-input"
                       lay-verify="name">
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-form-text" style="width: 1080px">
        <div class="layui-input-block">
            <textarea id="lay_edit" lay-verify="content" name="text"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-row">
        <div class="layui-col-md-offset5">
            <input type="submit" value="上传" class="layui-btn layui-border-black" lay-submit=""
                   lay-filter="forSubmit"/>
            <input type="reset" value="重置" class="layui-btn layui-btn-primary layui-border-black"/>
        </div>
    </div>
</form>
<script src="${ctx}/static/js/jquery.min.js"></script>
<script src="${ctx}/static/layui/layui.js"></script>
<script src="${ctx}/static/js/index.js"></script>
<script>
    layui.use(['layedit', 'form', 'jquery'], function () {
        let form = layui.form;
        var layedit = layui.layedit;
        let $ = layui.$;

        //创建一个编辑器，lay_edit为textarea标签的id值
        let index = layedit.build('lay_edit', {
            height: 350    //设置编辑器的高度
            //tool: []
        });
        //提交时把值同步到文本域中
        form.verify({
            name: function (value) {
                if (value.length == 0) {
                    return "标题不能为空";
                }
            },
            //content富文本域中的lay-verify值
            content: function (value) {
                return layedit.sync(index);
            }
        });

        //重置功能
        $("input[value='重置']").on("click", function () {
            layedit.setContent(index, "");
        });
        //上传提交
        form.on("submit(forSubmit)", function (data) {
            let content = layedit.getContent(index);
            //let content = layedit.getText(index);
            let name = $("#name").val();
            layer.confirm("确定要上传吗？", {btn: ['确定', '取消'], title: "提示"}, function () {
                $.ajax({
                    url: "${ctx}/student/uploadInternshipLog.action",
                    type: 'post',
                    data: {content: content, name: name},
                    dataType: "json",
                    contentType: "application/x-www-form-urlencoded",
                    success: function (data) {
                        //console.log(data);
                        if (data.flag) {
                            layer.msg("上传成功");
                        } else {
                            layer.msg("上传失败");
                        }
                        setTimeout(() => {
                            window.location.reload()
                        }, 1500);
                    },
                    error: function () {
                        layer.msg("请求失败");
                    }
                });
            });
            return false;
        })
    });
</script>
</body>
</html>